package com.fsharemobile.adapter;

import java.util.List;

import com.fsharemobile.R;
import com.fsharemobile.client.File;
import com.fsharemobile.fragments.ExplorerFragment;
import com.kenny.mypopupwindow.ActionItem;
import com.kenny.mypopupwindow.QuickAction;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class ExplorerArrayAdapter extends ArrayAdapter<File> {

	private static boolean LISTVIEW_LIST=false;
	
	private Context context;
	List<File> arr= null;
	public static QuickAction myQuickAc;
	private int pos;
	static class ViewHolder {
	    public TextView text;
	    public ImageView image;
	    public ImageButton button;
	  }
	
	public ExplorerArrayAdapter(Context context, int resource,
			List<File> objects) {
		super(context, R.layout.my_item_explorer_layout, objects);
		// TODO Auto-generated constructor stub
		this.context=context;
		this.arr=objects;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		pos=position;
		View rowView= convertView;
		if(rowView==null){
			LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			//inflater.inflate(R.layout.my_item_explorer_layout, null);
			rowView= inflater.inflate(R.layout.my_item_explorer_layout, parent, false);
			
			ViewHolder viewHolder= new ViewHolder();
			viewHolder.text= (TextView) rowView.findViewById(R.id.txtExplorer);
			viewHolder.image =(ImageView) rowView.findViewById(R.id.imgExplorer);
			viewHolder.button= (ImageButton) rowView.findViewById(R.id.btn_functions);
			viewHolder.button.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.ic_arrow_down));
			rowView.setTag(viewHolder);			
		}
		
		final ViewHolder holder= (ViewHolder) rowView.getTag();
		final File files= arr.get(pos);
		String fileName = files.getmName();
		String type= files.getmType();
		//set ...
		holder.text.setText(fileName);
		if(type.compareTo("01")==0){
			holder.image.setImageResource(R.drawable.ic_camera);
		}else if(type.compareTo("02")==0){
			holder.image.setImageResource(R.drawable.ic_view_select);
		}else if(type.compareTo("03")==0){
			holder.image.setImageResource(R.drawable.ic_folder);
		}else{
			holder.image.setImageResource(R.drawable.ic_launcher);
		}
		getContext().getResources().getDrawable(R.drawable.ic_arrow_down);
		holder.button.setOnClickListener(new View.OnClickListener() {
			
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(ExplorerFragment.quickAc!=null){
					ExplorerFragment.quickAc.dismiss();
				}
				if(LISTVIEW_LIST==false){
					
						myQuickAc = new QuickAction(getContext(), QuickAction.HORIZONTAL);
						ActionItem itemDelete =  new ActionItem(1, null, getContext().getResources().getDrawable(R.drawable.ic_recycle));
						ActionItem itemShare =  new ActionItem(2, null, getContext().getResources().getDrawable(R.drawable.ic_share));
						ActionItem itemModifier =  new ActionItem(3, null, getContext().getResources().getDrawable(R.drawable.ic_check));
						myQuickAc.addActionItem(itemDelete);
						myQuickAc.addActionItem(itemShare);
						myQuickAc.addActionItem(itemModifier);
						myQuickAc.show(v);
						myQuickAc.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
							
							@Override
							public void onItemClick(QuickAction source, int pos, int actionId) {
								// TODO Auto-generated method stub
								if(actionId==1){
									Toast.makeText(context, "this is delete function", Toast.LENGTH_SHORT).show();
								}else if (actionId==2) {
									Toast.makeText(context, "this is share function", Toast.LENGTH_SHORT).show();
								}else if(actionId==3){
									Toast.makeText(context, "this is modifier function", Toast.LENGTH_SHORT).show();
								}
								LISTVIEW_LIST=false;
								holder.button.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.ic_arrow_down));
								myQuickAc.dismiss();
							}
						});
						holder.button.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.ic_arrow_up));
						LISTVIEW_LIST=true;
						
					
				
				}else{
					LISTVIEW_LIST=false;
					holder.button.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.ic_arrow_down));
					myQuickAc.dismiss();
					
				}	
			}
		});	
		return rowView;
	}
	
	
	
	
	
	
	

	

}
